<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>参会企业</span>
      </div>
      <div class="list-search">
        <el-select
          v-model="audit"
          placeholder="预定状态"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限预定状态" value="" />
          <el-option label="预定成功" value="1" />
          <el-option v-if="auditWait<=0" label="等待审核" value="2" />
          <el-option v-if="auditWait>0" label="等待审核" value="2">
            <span style="float: left">等待审核</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ auditWait }}</span>
          </el-option>
          <el-option label="未通过" value="3" />
        </el-select>
        <el-select
          v-model="recommend"
          placeholder="推荐名企"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限推荐名企" value="" />
          <el-option label="已推荐" value="2" />
          <el-option label="未推荐" value="1" />
        </el-select>
        <el-select
          v-model="jobfair_id"
          placeholder="招聘会"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限招聘会" value="" />
          <el-option v-for="(item,index) in jobfair_list" :key="index" :label="item.title" :value="item.id" />
        </el-select>
        <el-select
          v-model="settr"
          placeholder="添加时间"
          class="list-options"
          @change="funSearch"
        >
          <el-option label="不限添加时间" value="" />
          <el-option label="三天内" value="3" />
          <el-option label="一周内" value="7" />
          <el-option label="一月内" value="30" />
          <el-option label="半年内" value="180" />
          <el-option label="一年内" value="360" />
        </el-select>
        <el-input
          v-model="keyword"
          placeholder="请输入搜索内容"
          class="input-with-select"
          @keyup.enter.native="funSearchKeyword"
        >
          <el-select
            slot="prepend"
            v-model="key_type"
            placeholder="请选择"
            class="input-sel"
          >
            <el-option label="公司" value="1" />
            <el-option label="招聘会" value="2" />
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="funSearchKeyword"
          />
        </el-input>
      </div>
      <div class="spaceline" />
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="42" />
        <el-table-column label="参会企业" show-overflow-tooltip min-width="200">
          <template slot-scope="scope">
            <el-link :href="scope.row.company_link" target="_blank" type="primary">
              {{ scope.row.companyname }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="招聘会标题" show-overflow-tooltip min-width="200">
          <template slot-scope="scope">
            <el-link :href="scope.row.jobfair_link" target="_blank" type="primary">
              {{ scope.row.jobfair_title }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="预定展位" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.position }}
          </template>
        </el-table-column>
        <el-table-column label="预定状态" width="90">
          <template slot-scope="scoped">
            <span v-if="scoped.row.audit == 1" style="color:#67c23a;">预定成功</span>
            <span v-if="scoped.row.audit == 2" style="color:#e6a23c;">等待审核</span>
            <span v-if="scoped.row.audit == 3" style="color:#f56c6c;">未通过</span>
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" width="90">
          <template slot-scope="scope">
            {{ scope.row.contact }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话" align="center" width="120">
          <template slot-scope="scope">
            {{ scope.row.mobile }}
          </template>
        </el-table-column>
        <el-table-column label="推荐名企" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_recommend"
              @change="modifyRecommend(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="created_at"
          label="添加日期"
          width="150"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ scope.row.eaddtime | timeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="funEdit(scope.$index, scope.row)"
            >
              修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="funDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="spaceline" />
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button size="small" type="primary" @click="goAdd">
            新增
          </el-button>
          <el-button size="small" type="primary" @click="funAudit">
            参会状态
          </el-button>
          <el-button size="small" type="primary" @click="funRecommend">
            名企推荐
          </el-button>
          <el-button size="small" type="danger" @click="funDeleteSelect">
            删除所选
          </el-button>
        </el-col>
        <el-col :span="16" style="text-align: right;">
          <el-pagination
            background
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
    </el-card>
    <el-dialog title="将所选参会企业设置为" :visible.sync="dialogFormVisible" width="25%">
      <el-form class="common-form" label-width="80px">
        <el-form-item label="预定状态">
          <el-radio-group v-model="setAuditVal">
            <el-radio :label="1">预定成功</el-radio>
            <el-radio :label="2">等待审核</el-radio>
            <el-radio :label="3">审核未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="setAuditVal == 3" label="原因">
          <el-input v-model="setAuditNote" type="textarea" rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="funSetAudit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="将所选参会企业设置为推荐企业" :visible.sync="dialogFormRecommend" width="25%">
      <el-form class="common-form" label-width="80px">
        <el-form-item label="名企推荐">
          <el-radio-group v-model="setRecommendVal">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormRecommend = false">取 消</el-button>
        <el-button type="primary" @click="funSetRecommend">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  jobfairListAll,
  exhibitorsList,
  exhibitorsDelete,
  exhibitorsAudit,
  exhibitorsRecommend
} from '@/api/jobfair'
import { parseTime } from '@/utils/index'
export default {
  filters: {
    timeFilter(timestamp) {
      return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
    },
    auditFilter(audit) {
      switch (audit) {
        case 1:
          return 'success'
        case 2:
          return 'danger'
        case 3:
          return 'info'
        default:
          return ''
      }
    }
  },
  data() {
    return {
      audit: '',
      recommend: '',
      jobfair_id: '',
      settr: '',
      keyword: '',
      key_type: '1',
      jobfair_list: [],
      auditWait: 0,
      total: 0,
      currentPage: 1,
      pagesize: 10,
      listLoading: !1,
      list: [],
      tableIdarr: [],
      auditSubmitLoading: !1,
      setAuditVal: 1,
      setAuditNote: '',
      dialogFormVisible: !1,
      setRecommendVal: 1,
      dialogFormRecommend: !1
    }
  },
  created(){
    this.jobfair_id = this.$route.query.id
    this.getJobfair()
    this.getExhibitors()
  },
  methods: {
    getJobfair: function(){
      jobfairListAll().then(response => {
        this.jobfair_list = response.data.items
      })
    },
    getExhibitors: function(){
      this.listLoading = true
      const param = {
        audit: this.audit,
        recommend: this.recommend,
        jobfair_id: this.jobfair_id,
        settr: this.settr,
        key_type: this.key_type,
        keyword: this.keyword,
        page: this.currentPage,
        pagesize: this.pagesize
      }
      exhibitorsList(param).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.total = response.data.total
        this.currentPage = response.data.current_page
        this.pagesize = response.data.pagesize
      })
    },
    funSearch: function(){
      this.getExhibitors()
    },
    funSearchKeyword: function(){
      this.currentPage = 1
      this.getExhibitors()
    },
    handleSizeChange: function(val){
      this.pagesize = val
      this.getExhibitors()
    },
    handleCurrentChange: function(val){
      this.currentPage = val
      this.getExhibitors()
    },
    handleSelectionChange(idlist) {
      this.tableIdarr = []
      if (idlist.length > 0) {
        for (const item of idlist) {
          this.tableIdarr.push(item.id)
        }
      }
    },
    funEdit: function(index, row){
      this.$router.push({
        path: '/jobfair/exhibitors/edit',
        query: {
          id: row.id
        }
      })
    },
    goAdd: function(){
      this.$router.push('/jobfair/exhibitors/add')
    },
    funAudit: function(){
      if (this.tableIdarr.length == 0) {
        this.$message.error('请选择参会企业')
        return false
      }
      this.auditIdarr = this.tableIdarr
      this.setAuditVal = 1
      this.setAuditNote = ''
      this.dialogFormVisible = !0
    },
    funSetAudit: function(){
      if (this.auditSubmitLoading == true) return false
      this.auditSubmitLoading = true
      const params = {
        id: this.auditIdarr,
        audit: this.setAuditVal,
        note: this.setAuditNote
      }
      exhibitorsAudit(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.getExhibitors()
        } else {
          this.$message.error(response.message)
        }
        this.dialogFormVisible = !1
        this.auditSubmitLoading = !1
      }).catch(() => {
        this.dialogFormVisible = !1
        this.auditSubmitLoading = !1
      })
    },
    funRecommend: function(){
      if (this.tableIdarr.length == 0) {
        this.$message.error('请选择参会企业')
        return false
      }
      this.auditIdarr = this.tableIdarr
      this.setRecommendVal = 1
      this.dialogFormRecommend = !0
    },
    funSetRecommend: function(){
      if (this.auditSubmitLoading == true) return false
      this.auditSubmitLoading = true
      const params = {
        id: this.auditIdarr,
        recommend: this.setRecommendVal
      }
      exhibitorsRecommend(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.getExhibitors()
        } else {
          this.$message.error(response.message)
        }
      }).catch(() => {
        this.getExhibitors()
      })
      this.dialogFormRecommend = !1
      this.auditSubmitLoading = !1
    },
    funDelete: function(index, row){
      var that = this
      that
        .$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: row.id
          }
          exhibitorsDelete(param).then(response => {
            that.$message.success(response.message)
            that.getExhibitors()
            return true
          })
        })
        .catch(() => {})
    },
    funDeleteSelect: function(){
      var that = this
      if (that.tableIdarr.length == 0) {
        that.$message.error('请选择要删除的数据')
        return false
      }
      that
        .$confirm('此操作将永久删除选中的数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: that.tableIdarr
          }
          exhibitorsDelete(param).then(response => {
            that.$message.success(response.message)
            that.getExhibitors()
            return true
          })
        })
        .catch(() => {})
    },
    modifyRecommend(row) {
      const params = {
        id: row.id,
        recommend: row.is_recommend === true ? 1 : 0
      }
      exhibitorsRecommend(params).then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.getExhibitors()
        } else {
          this.$message.error(response.message)
          this.getExhibitors()
        }
        return true
      }).catch(() => {
        this.getExhibitors()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .qrcode{vertical-align:middle}
</style>
